package com.sz.hiling.controller;

import com.sz.hiling.mapper.UserMapper;
import com.sz.hiling.model.User;
import com.sz.hiling.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/")
    public String getAllUsers(Model model){
        model.addAttribute("users",userService.findAllUsers());
        return "home";
    }
    @GetMapping("/deleteUserById/{userid}")
    public String removeUserById(@PathVariable("userid") Integer userid, Model model, HttpServletRequest request){
        String admins = (String) request.getAttribute("admins");
        String msg=null;
        if (admins==null){
            return msg="你无权删除";
        }else {
            int n = userService.deleteUserById(userid);
            msg = n > 0 ? "删除成功" : "删除失败";
        }
         model.addAttribute("msg",msg);
        model.addAttribute("users",userService.findAllUsers());
        return "home";
    }

}
